package org.dromara.module.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.module.domain.Product;
import org.dromara.module.domain.ProductAttrValue;
import org.dromara.module.domain.ProductOrderInfo;
import org.dromara.module.mapper.ProductAttrValueMapper;
import org.dromara.module.mapper.ProductMapper;
import org.dromara.module.mapper.ProductOrderInfoMapper;
import org.dromara.module.service.IProductOrderInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.Date;
import java.util.List;

/**
 * 订单详情内容  订单里面的 商品列Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMapper, ProductOrderInfo> implements IProductOrderInfoService {

    @Resource
    private ProductOrderInfoMapper productOrderInfoMapper;
    @Resource
    private ProductAttrValueMapper productAttrValueMapper;
    @Resource
    private ProductMapper productMapper;


    @Value("${ruoyi.imgUrl}")
    private String imgPrefix;//图片地址
    @Value("${ruoyi.profile}")
    private String uploadPath;


    @Override
    public List<ProductOrderInfo> selectListByOrderId(Long order_id) {

        QueryWrapper<ProductOrderInfo> qwInfos = new QueryWrapper<>();
        qwInfos.eq("order_id", order_id);
//        qwInfos.eq("state",1);
        qwInfos.eq("is_del", 0);
        List<ProductOrderInfo> infos = productOrderInfoMapper.selectList(qwInfos);
        if(infos != null && infos.size() > 0) {
            for (ProductOrderInfo orderInfo : infos) {
                if(StrUtil.isEmpty(orderInfo.getSkuName())) {
                    ProductAttrValue attrValue = productAttrValueMapper.selectById(orderInfo.getSkuId());
                    if (attrValue != null) {
                        orderInfo.setSkuName(attrValue.getSku());
                    }
                }

                Product product = productMapper.selectById(orderInfo.getProductId());
                if (product != null) {
                    orderInfo.setProductName(product.getProductName());
                    orderInfo.setImgUrl(imgPrefix + product.getImage());
                }
            }
        }
        return infos;
    }


    @Override
    public ProductOrderInfo selectOneByOrderId(Long order_id) {
        QueryWrapper<ProductOrderInfo> qwInfos = new QueryWrapper<>();
        qwInfos.eq("order_id", order_id);
        qwInfos.eq("state",1);
        qwInfos.eq("is_del", 0);
        qwInfos.last("limit 1");
        return productOrderInfoMapper.selectOne(qwInfos);
    }
}
